/* kernel/task.c 
 * 
 * Copyright (C) 2021, 2022 intirain. 
 *
 * This file is part of evx. 
 * 
 * evx is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version. 
 * 
 * evx is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * GNU General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License 
 * along with evx. If not, see <https://www.gnu.org/licenses/>
 */ 




#include <kernel/init.h>
#include <kernel/kernel.h>
#include <kernel/module.h>
#include <kernel/mm.h>
#include <kernel/task.h>
#include <kernel/kmlog.h>

#include <arch/archinfo.h>

#include <sys/types.h>


struct task init_task = {
	.state = TASK_RUNNING, 
	.pid = 0, 
	.parent = NULL, 
	.arch_descr = ARCH_TASK_DESCR_INIT, 
	.nice = 0
};

struct task *current = &init_task;
int next_pid = 1;

int task_init(void) {
//	struct task *new = dup_task();

//	kmlog("New task: %p\n", new);
//	kmlog("task initialization finished. \n");

//	switch_to(new);

	return 0;
}

struct task *dup_task(void) {
	struct task *tsk;

	if (unlikely(!(tsk = kmalloc(sizeof(struct task))))) 
		panic("dup_task: Out of memory");

	tsk->state = TASK_WAIT;
	tsk->pid = next_pid++;
	tsk->parent = current;
	tsk->nice = 0;

	arch_task_descr_init(&tsk->arch_descr);
	arch_stack_init(&tsk->arch_descr);
	tsk->state = TASK_READY;

	return tsk;
}

kernel_init(task_init);

